﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;


namespace HITGPA
{
    class dawu
    {
        public enum Class_Name_Enum
        {
            全息照相=1,
            太阳能电池=2,
            线阵CCD=3,
            金属电子逸出功=4,
            光电效应=5,
            RC电路的研究=6,
            动态磁滞回线=7,
            迈克尔逊干涉仪=8,
            分光计=9,
            偏振光=10,
            声速的测定=11
        }
        /// <summary>
        /// 通过get方式请求页面，传递一个实例化的cookieContainer
        /// </summary>
        /// <param name="postUrl"></param>
        /// <param name="cookie"></param>
        /// <returns></returns>
        public static ArrayList GetHtmlData(string postUrl, CookieContainer cookie)
        {
            HttpWebRequest request;
            HttpWebResponse response;
            ArrayList list = new ArrayList();
            request = (HttpWebRequest)WebRequest.Create(postUrl);
            request.Method = "GET";
            request.ContentType = "	text/html; charset=utf-8";
            request.Referer = "http://172.31.65.65/booking/admin_left.aspx";
            request.UserAgent = "Mozilla/5.0 (Windows NT 6.2; WOW64; rv:36.0) Gecko/20100101 Firefox/36.0";
            request.CookieContainer = cookie;
            request.KeepAlive = true;
            //request.Timeout = 1000;
            //request.CookieContainer = cookie;
            try
            {
                //获取服务器返回的资源
                using (response = (HttpWebResponse)request.GetResponse())
                {
                    using (StreamReader reader = new StreamReader(response.GetResponseStream(), Encoding.GetEncoding("utf-8")))
                    {
                        cookie.Add(response.Cookies);
                        //保存Cookies
                        list.Add(cookie);
                        list.Add(reader.ReadToEnd());
                    }
                }
            }
            catch (WebException ex)
            {

                list.Clear();
                list.Add("发生异常/n/r");
                //WebResponse wr = ex.Response;
                //using (Stream st = wr.GetResponseStream())
                //{
                //    using (StreamReader sr = new StreamReader(st, System.Text.Encoding.Default))
                //    {
                //        list.Add(sr.ReadToEnd());
                //    }
                //}
            }
            catch (Exception ex)
            {
                list.Clear();
                list.Add("5");
                list.Add("发生异常：" + ex.Message);
            }
            return list;
        }

        /// <summary>
        /// 发送相关数据至页面
        /// 进行登录操作
        /// 并保存cookie
        /// </summary>
        /// <param name="postData"></param>
        /// <param name="postUrl"></param>
        /// <param name="cookie"></param>
        /// <returns></returns>
        public static ArrayList PostData(string postData, string postUrl, CookieContainer cookie)
        {
            ArrayList list = new ArrayList();
            HttpWebRequest request;
            HttpWebResponse response;
            UTF8Encoding encoding = new UTF8Encoding();
            request = (HttpWebRequest)WebRequest.Create(postUrl);

            byte[] b = encoding.GetBytes(postData);
            request.ContentType = "application/x-www-form-urlencoded; charset=UTF-8";
            request.UserAgent = "Mozilla/5.0 (Windows NT 6.2; WOW64; rv:36.0) Gecko/20100101 Firefox/36.0";
            request.Method = "POST";
            request.CookieContainer = cookie;
            request.ContentLength = b.Length;
            try
            {
                using (Stream stream = request.GetRequestStream())
                {
                    stream.Write(b, 0, b.Length);
                }
            }
            catch (Exception ex)
            {
                System.Windows.MessageBox.Show(ex.Message);
                list.Add(ex.Message);
                return list;
                
            }

            try
            {
                //获取服务器返回的资源
                using (response = request.GetResponse() as HttpWebResponse)
                {
                    using (StreamReader reader = new StreamReader(response.GetResponseStream(), Encoding.GetEncoding("utf-8")))
                    {
                        if (response.Cookies.Count > 0)
                            cookie.Add(response.Cookies);
                        //list.Add(cookie);
                        list.Add(reader.ReadToEnd());
                    }
                }
            }
            catch (WebException wex)
            {
                WebResponse wr = wex.Response;
                using (Stream st = wr.GetResponseStream())
                {
                    using (StreamReader sr = new StreamReader(st, System.Text.Encoding.Default))
                    {
                        list.Add(sr.ReadToEnd());
                    }
                }
            }
            catch (Exception ex)
            {
                list.Add(ex.Message);
            }

            return list;
        }

        public static string AutoBook(CookieContainer dawu_cookie, string postdata1, string postdata2,ObservableCollection<Is_Class_Book> list)
        {
            int index_exp = 1;
            string result_str = null;
            string postdata;
            while (result_str != "yes选课成功")
            {
                if (result_str == "已达本周最多预约数")
                    return result_str;
                if (index_exp >= 12)
                {
                    return "选课失败！";
                }
                if (list[index_exp - 1].isBook == false)
                {
                    index_exp++;
                    continue;
                }
                else
                {
                    postdata = postdata1 + Convert.ToSingle(index_exp) + postdata2;
                    result_str = (string)dawu.PostData(postdata, "http://172.31.65.65/booking/forajax.aspx", dawu_cookie)[0];
                    index_exp = index_exp + 1;
                }
            }
            if (index_exp - 1 == 1)
                return "选课成功！ 全息照相(N126)";
            else if (index_exp - 1 == 2)
                return "选课成功！ 太阳能电池(N134)";
            else if (index_exp - 1 == 3)
                return "选课成功！ 线阵CCD(N135)";
            else if (index_exp - 1 == 4)
                return "选课成功！ 金属电子逸出功(N141)";
            else if (index_exp - 1 == 5)
                return "选课成功！ 光电效应(N141)";
            else if (index_exp - 1 == 6)
                return "选课成功！ RC电路的研究(N144)";
            else if (index_exp - 1 == 7)
                return "选课成功！ 动态磁滞回线(N144)";
            else if (index_exp - 1 == 8)
                return "选课成功！ 迈克尔逊干涉仪(N149)";
            else if (index_exp - 1 == 9)
                return "选课成功！ 分光计(N145)";
            else if (index_exp - 1 == 10)
                return "选课成功！ 偏振光(N148)";
            else
                return "选课成功！ 声速的测定(N148)";

        }

    }


    public class Week : ObservableCollection<string>
    {
        public Week()
            : base()
        {
            Add("1");
            Add("2");
            Add("3");
            Add("4");
            Add("5");
            Add("6");
            Add("7");
            Add("8");
            Add("9");
            Add("10");
            Add("11");
            Add("12");
            Add("13");
            Add("14");
            Add("15");
            Add("16");
            Add("17");
        }
    }
    public class Day : ObservableCollection<string>
    {
        public Day()
            : base()
        {
            Add("1");
            Add("2");
            Add("3");
            Add("4");
            Add("5");
            Add("6");
            Add("7");
        }
    }

    public class Class_Time : ObservableCollection<string>
    {
        public Class_Time()
            : base()
        {
            Add("1-2");
            Add("3-4");
            Add("5-6");
            Add("7-8");
            Add("9-10");
        }
    }

    public class Is_Class_Book
    {
        private bool _isBook;
        private string _class_Name;
        public bool isBook 
        {
            get
            {
                return _isBook;
            }
            set
            {
                _isBook=value;
            }
        }
        public string class_Name 
        {
            get
            {
                return _class_Name;
            }
            set
            {
                _class_Name = value;
            }
        }
    }

}
